<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="com.uc.training.smadmin.sys.dao.SysMenuDao">
    <!-- 根据用户id获取用户权限标识 -->
    <select id="getUserPerms" resultClass="java.lang.String" parameterClass="java.lang.Long">
        SELECT
			DISTINCT(m.rel)
		FROM
			t_sys_menu m,t_sys_user_role ur,t_sys_role_menu rm
		WHERE
			ur.role_id = rm.role_id
		AND
			rm.menu_id = m.id
		AND
			ur.user_id = #userId#
    </select>

	<!-- 获取所有的菜单 -->
	<select id="getMenuList" resultClass="com.uc.training.smadmin.sys.model.SysMenu">
		SELECT
		 m.id AS id,
		 m.name AS name,
		 m.type AS type,
		 m.level AS level,
		 m.parent_id AS parentId,
		 m.sort_num AS sortNum,
		 m.url AS url,
		 m.rel AS rel
		FROM t_sys_menu m
		ORDER BY m.sort_num
	</select>

  <!-- 通过菜单ID获取菜单 -->
  <select id="getById" parameterClass="Long" resultClass="com.uc.training.smadmin.sys.model.SysMenu">
    SELECT
	  m.id AS id,
	  m.name AS name,
	  m.type AS type,
	  m.level AS level,
	  m.parent_id AS parentId,
	  m.sort_num AS sortNum,
	  m.url AS url,
	  m.rel AS rel
	FROM t_sys_menu m
	WHERE id = #id#
  </select>

  <!-- 插入菜单 -->
  <insert id="addMenu" parameterClass="com.uc.training.smadmin.sys.model.SysMenu">
    INSERT INTO t_sys_menu
    (parent_id, name, url, level, rel, type, sort_num, create_emp)
    values (#parentId#, #name#, #url#, #level#, #rel#, #type#, #sortNum#, #createEmp#)
    <selectKey resultClass="Long" keyProperty="id">
      SELECT LAST_INSERT_ID() AS id
    </selectKey>
  </insert>

  <!-- 通过主键更新 -->
  <update id="updateById" parameterClass="com.uc.training.smadmin.sys.model.SysMenu">
    UPDATE t_sys_menu SET
    name = #name#, url = #url#, rel = #rel#, sort_num = #sortNum#, modify_emp = #modifyEmp#
    WHERE id = #id#
  </update>

  <!-- 通过ID删除菜单 -->
  <delete id="deleteById" parameterClass="Long">
    DELETE FROM t_sys_menu
    WHERE id = #id#
  </delete>

  <select id="getListByParentId" parameterClass="Long" resultClass="Long">
	  SELECT id
	  FROM t_sys_menu
	  WHERE parent_id = #id#
  </select>

  <!-- 根据父节点ID级联删除子节点 -->
  <delete id="batchDelete" parameterClass="java.util.List">
	  DELETE FROM t_sys_menu
	  WHERE id IN
	  <iterate open="(" close=")" conjunction=",">
		#ids[]#
	  </iterate>
  </delete>

  <!-- 根据被删除的菜单ID删除角色菜单权限 -->
  <delete id="deleteRoleMenuByMenuList" parameterClass="java.util.List">
	  DELETE FROM t_sys_role_menu
	  WHERE menu_id IN
	  <iterate open="(" close=")" conjunction=",">
		#mid[]#
	  </iterate>
  </delete>

	<!-- 通过角色名查找用户数量 -->
	<select id="queryCountByName" resultClass="java.lang.Integer" parameterClass="String">
    SELECT count(1)
    FROM t_sys_menu s
    WHERE s.name = #name#
  </select>

  <select id="getIdList" resultClass="Long">
	  SELECT s.id
	  FROM t_sys_menu s
  </select>
</sqlMap>
